package test;

import javax.swing.tree.TreeNode;

/**
 * @ClassName Test
 * @Description
 * @Author ZJX
 * @Date 2025/3/2 18:47
 * @Version 1.0
 **/
public class  Solution{

        // 递归反转链表的方法
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode newHead = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return newHead;
        }

        // 创建链表并打印的方法
        public ListNode createLinkedList(int[] arr) {
            ListNode dummy = new ListNode(0);  // 创建一个虚拟头节点，方便操作
            ListNode current = dummy;
            for (int val : arr) {
                current.next = new ListNode(val);
                current = current.next;
            }
            return dummy.next;  // 返回真正的头节点
        }

        // 打印链表的方法
        public void printLinkedList(ListNode head) {
            while (head != null) {
                System.out.print(head.val + " ");
                head = head.next;
            }
            System.out.println();
        }

        public static void main(String[] args) {
            Solution solution = new Solution();

            // 测试用例 1: 普通链表
            int[] arr1 = {1, 2, 3, 4, 5};
            ListNode head1 = solution.createLinkedList(arr1);
            System.out.print("原链表: ");
            solution.printLinkedList(head1);
            ListNode reversedHead1 = solution.reverseList(head1);
            System.out.print("反转后的链表: ");
            solution.printLinkedList(reversedHead1);

            // 测试用例 2: 链表为空
            ListNode head2 = null;
            System.out.print("原链表为空: ");
            solution.printLinkedList(head2);
            ListNode reversedHead2 = solution.reverseList(head2);
            System.out.print("反转后的链表: ");
            solution.printLinkedList(reversedHead2);

            // 测试用例 3: 链表只有一个元素
            int[] arr3 = {10};
            ListNode head3 = solution.createLinkedList(arr3);
            System.out.print("原链表: ");
            solution.printLinkedList(head3);
            ListNode reversedHead3 = solution.reverseList(head3);
            System.out.print("反转后的链表: ");
            solution.printLinkedList(reversedHead3);
        }
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public int max(int[] arr){
            return MaxR(arr,0,arr.length-1);
    }
    public int MaxR(int[] arr,int L,int R){
            if(L==R){
                    return arr[L];
            }
            int mid = L + (R-L)/2;
            int leftMax = MaxR(arr,L,mid);
            int rightMax = MaxR(arr,mid+1,R);
            return Math.max(leftMax,rightMax);
    }


}
